//
//  AddOrderFooterView.m
//  BaseFrame
//
//  Created by 孟学浩 on 2017/5/12.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "AddOrderFooterView.h"

@interface AddOrderFooterView()

@property (nonatomic,strong) UILabel *orderIdLabel;
@property (nonatomic,strong) UILabel *timeLabel;


@end

@implementation AddOrderFooterView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {        
        

    }
    return self;
}

- (void)timeScoreEvent:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (self.selectTimeScoreHandle) {
        self.selectTimeScoreHandle();
    }
}

- (void)addressCkick{
    NSLog(@"选择地址");
    if (_getAddressClick) {
        _getAddressClick();
    }
}

- (void)yongjiujifenClick:(UIButton *)sender{
    if ([self.model[@"commodity_integral"] intValue] > [self.model[@"use_limit_integral"] intValue]) {
        [self makeToast:@"当前积分不足以购买商品，请选择其他方式" duration:2 position:CSToastPositionCenter];
        return;
    }
    sender.selected = !sender.selected;
    if (_selectAllScoreHandle) {
        _selectAllScoreHandle();
    }
}

- (void)evetScoreEvent:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (self.selectEverScoreHandle) {
        self.selectEverScoreHandle();
    }
}

- (void)setModel:(NSDictionary *)model{
    _model = model;
    //订单号
    UIView *orderIdView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 24)];
    orderIdView.backgroundColor = [UIColor whiteColor];
    
    UILabel *orderIDTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, 24)];
    orderIDTitleLabel.font = DEF_FontSize_12;
    NSMutableAttributedString *attStr = [NSMutableAttributedString new];
    NSTextAttachment *imgAtt = [NSTextAttachment new];
    imgAtt.image = [UIImage imageNamed:@"order"];
    imgAtt.bounds = CGRectMake(0, -3, 12, 15);
    [attStr appendAttributedString:[NSAttributedString attributedStringWithAttachment:imgAtt]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"  订单号"]];
    orderIDTitleLabel.attributedText = attStr;
    
    UILabel *orderIDLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, 24)];
    orderIDLabel.font = DEF_FontSize_12;
    orderIDLabel.textAlignment = NSTextAlignmentRight;
    orderIDLabel.text = model[@"o_number"];
    
    [orderIdView addSubview:orderIDTitleLabel];
    [orderIdView addSubview:orderIDLabel];
    
    //时间
    UIView *timeView = [[UIView alloc] initWithFrame:CGRectMake(0, 28, SCREEN_WIDTH, 45)];
    timeView.backgroundColor = [UIColor whiteColor];
    UILabel *timeTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, SCREEN_WIDTH-40, 45)];
    timeTitleLabel.text = @"下单时间";
    timeTitleLabel.font = DEF_FontSize_12;
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, SCREEN_WIDTH-40, 45)];
    timeLabel.text = model[@"o_time"];
    timeLabel.textAlignment = NSTextAlignmentRight;
    timeLabel.font = DEF_FontSize_12;
    
    [timeView addSubview:timeTitleLabel];
    [timeView addSubview:timeLabel];
    
    //积分标题
    UIView *orderInfoVew = [[UIView alloc] initWithFrame:CGRectMake(0, 77, SCREEN_WIDTH, 24)];
    orderInfoVew.backgroundColor = [UIColor whiteColor];
    UILabel *orderInfoTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, 24)];
    orderInfoTitleLabel.font = DEF_FontSize_12;
    NSMutableAttributedString *attStr1 = [NSMutableAttributedString new];
    NSTextAttachment *imgAtt1 = [NSTextAttachment new];
    imgAtt1.image = [UIImage imageNamed:@"jifen1"];
    imgAtt1.bounds = CGRectMake(0, -3, 15, 15);
    [attStr1 appendAttributedString:[NSAttributedString attributedStringWithAttachment:imgAtt1]];
    [attStr1 appendAttributedString:[[NSAttributedString alloc] initWithString:@"  积分抵扣"]];
    orderInfoTitleLabel.attributedText = attStr1;
    
    [orderInfoVew addSubview:orderInfoTitleLabel];
    //选择积分
    UIView *selectView = [[UIView alloc] initWithFrame:CGRectMake(0, 105, SCREEN_WIDTH, 70)];
    selectView.backgroundColor = [UIColor whiteColor];
    
    
    [selectView addSubview:({
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(24, 0, SCREEN_WIDTH-60, 35)];
        label.font = DEF_FontSize_12;
        
        NSString *tmp = [NSString stringWithFormat:@"%@   (积分余额%@分，可抵%@元)",@"限时积分",model[@"use_permanent_points"],model[@"time_limited_integration"]];
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:tmp];
        [attStr addAttribute:NSForegroundColorAttributeName value:getColor(@"d73f3d") range:[tmp rangeOfString:@"限时积分"]];
        [attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16 weight:1] range:[tmp rangeOfString:@"限时积分"]];
        label.textColor = getColor(@"999999");
        label.attributedText = attStr;
//        view.userInteractionEnabled = YES;
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(timeScoreEvent)];
//        [view addGestureRecognizer:tap];
        UIButton *btn = [UIButton new];
        [btn setImage:[UIImage imageNamed:@"rect"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"ok2"] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(timeScoreEvent:) forControlEvents:UIControlEventTouchUpInside];
        btn.imageView.contentMode = UIViewContentModeScaleAspectFill;
        btn.size = CGSizeMake(40, 40);
        btn.center = CGPointMake(SCREEN_WIDTH-32, 35/2.);
        btn.selected = self.timeScore;
        [view addSubview:label];
        [view addSubview:btn];
        view;
    })];
    
    [selectView addSubview:({
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 35, SCREEN_WIDTH, 35)];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(24, 0, SCREEN_WIDTH-60, 35)];
        label.font = DEF_FontSize_12;
        
        NSString *tmp = [NSString stringWithFormat:@"%@   (积分余额%@分，可抵%@元)",@"永久积分",model[@"use_limit_integral"],model[@"ermanent_integral_bonus"]];
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:tmp];
        [attStr addAttribute:NSForegroundColorAttributeName value:getColor(@"3b76c2") range:[tmp rangeOfString:@"永久积分"]];
        [attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16 weight:1] range:[tmp rangeOfString:@"永久积分"]];
        label.textColor = getColor(@"999999");
        label.attributedText = attStr;
//        view.userInteractionEnabled = YES;
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(evetScoreEvent:)];
//        [view addGestureRecognizer:tap];
        UIButton *btn = [UIButton new];
        [btn setImage:[UIImage imageNamed:@"rect"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"ok2"] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(evetScoreEvent:) forControlEvents:UIControlEventTouchUpInside];
        btn.imageView.contentMode = UIViewContentModeScaleAspectFill;
        btn.size = CGSizeMake(40, 40);
        btn.center = CGPointMake(SCREEN_WIDTH-32, 35/2.);
        btn.selected = self.everScore;
        [view addSubview:label];
        [view addSubview:btn];
        view;
    })];
    
    UIView *integralToBuy = [[UIView alloc]initWithFrame:CGRectMake(0, 180, SCREEN_WIDTH, 24)];
    integralToBuy.backgroundColor = [UIColor whiteColor];
    [integralToBuy addSubview:({
        UILabel *integralToBuyLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, 24)];
        integralToBuyLabel.font = DEF_FontSize_12;
        NSMutableAttributedString *attStr2 = [NSMutableAttributedString new];
        NSTextAttachment *imgAtt2 = [NSTextAttachment new];
        imgAtt2.image = [UIImage imageNamed:@"in_buy"];
        imgAtt2.bounds = CGRectMake(0, -3, 15, 15);
        [attStr2 appendAttributedString:[NSAttributedString attributedStringWithAttachment:imgAtt2]];
        [attStr2 appendAttributedString:[[NSAttributedString alloc] initWithString:@"  积分购买"]];
        integralToBuyLabel.attributedText = attStr2;
        integralToBuyLabel;
    })];
//    address
    UIView *yongjiuzifen = [[UIView alloc]initWithFrame:CGRectMake(0, 209, SCREEN_WIDTH, 35)];
    yongjiuzifen.backgroundColor = [UIColor whiteColor];
    [yongjiuzifen addSubview:({
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(24, 0, SCREEN_WIDTH-60, 35)];
        label.font = DEF_FontSize_12;
        NSString *tmp = [NSString stringWithFormat:@"%@   (需要%d积分，剩余%@积分)",@"永久积分",[self.model[@"commodity_integral"] intValue],model[@"use_limit_integral"]];
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:tmp];
        [attStr addAttribute:NSForegroundColorAttributeName value:getColor(@"3b76c2") range:[tmp rangeOfString:@"永久积分"]];
        [attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16 weight:1] range:[tmp rangeOfString:@"永久积分"]];
        label.textColor = getColor(@"999999");
        label.attributedText = attStr;
//        view.userInteractionEnabled = YES;
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(yongjiujifenClick:)];
//        [view addGestureRecognizer:tap];
        UIButton *btn = [UIButton new];
        [btn setImage:[UIImage imageNamed:@"rect"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"ok2"] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(yongjiujifenClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.imageView.contentMode = UIViewContentModeScaleAspectFill;
        btn.size = CGSizeMake(40, 40);
        btn.center = CGPointMake(SCREEN_WIDTH-32, 35/2.);
        btn.selected = self.allScore;
        [view addSubview:label];
        [view addSubview:btn];
        view;
    })];
    
    UIView *addressView = [[UIView alloc]initWithFrame:CGRectMake(0, 249, SCREEN_WIDTH, 24)];
    addressView.backgroundColor = [UIColor whiteColor];
    [addressView addSubview:({
        UILabel *integralToBuyLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, 24)];
        integralToBuyLabel.font = DEF_FontSize_12;
        NSMutableAttributedString *attStr2 = [NSMutableAttributedString new];
        NSTextAttachment *imgAtt2 = [NSTextAttachment new];
        imgAtt2.image = [UIImage imageNamed:@"address"];
        imgAtt2.bounds = CGRectMake(0, -3, 15, 15);
        [attStr2 appendAttributedString:[NSAttributedString attributedStringWithAttachment:imgAtt2]];
        [attStr2 appendAttributedString:[[NSAttributedString alloc] initWithString:@"  收货地址"]];
        integralToBuyLabel.attributedText = attStr2;
        integralToBuyLabel;
    })];
    [addressView addSubview:({
        UIImageView *moreImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 25, 0, 8, 24)];
        moreImageView.contentMode = UIViewContentModeScaleAspectFit;
        moreImageView.image = [UIImage imageNamed:@"more"];
        moreImageView;
    })];
    addressView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(addressCkick)];
    [addressView addGestureRecognizer:tap];
    
    UIView *addressContent = [[UIView alloc]initWithFrame:CGRectMake(0, 278, SCREEN_WIDTH, 35)];
    addressContent.backgroundColor = [UIColor whiteColor];
    [addressContent addSubview:({
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(24, 0, SCREEN_WIDTH-60, 35)];
        label.font = DEF_FontSize_12;
        label.numberOfLines = 0;
        label.text = self.address;
        [view addSubview:label];
        view;
    })];
    addressContent.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(addressCkick)];
    [addressContent addGestureRecognizer:tap1];

    [self addSubview:orderIdView];
    [self addSubview:timeView];
    [self addSubview:orderInfoVew];
    [self addSubview:selectView];
    [self addSubview:integralToBuy];
    [self addSubview:yongjiuzifen];
    [self addSubview:addressView];
    [self addSubview:addressContent];
}

@end
